<template>

  <el-cascader
    :value="value"
    :props="defaultProps"
    :options="options"
    separator="|"
    class="inputW"
    @change="handleChange"
  />

</template>

<script>
import { getDepartment } from '@/api/department'
import { arr2tree } from '@/utils'
export default {
  props: {
    value: {
      type: Number, // 存储的是部门的id  3 4 5
      // required: true,
      default: null
    }
  },
  data() {
    return {
      // value: [], // 选中项绑定值
      defaultProps: {
        value: 'id',
        label: 'name'
      },
      options: []// 数组
    }
  },
  created() {
    this.loadDepartment()
  },
  methods: {
    handleChange(value) {
      console.log(value, '是一个数组')
      const id = value[value.length - 1]
      console.log(id)
      // 子传父
      this.$emit('input', id)
    },
    // 获取部门列表
    async loadDepartment() {
      const res = await getDepartment()
      // console.log(res)
      // 数组转树
      this.options = arr2tree(res.data, 0)
    }
  }
}
</script>

<style>

</style>
